<template>
  <div class="app-container">
    <!-- 功能区 -->
    <transition name="formFade">
      <el-card class="operate-container" shadow="never" v-if="showSearch">
        <el-form :inline="true" :model="listQuery" @submit.native.prevent>
          <el-form-item label="工程名称" class="form-item">
            <el-input v-model="listQuery.projectName" clearable class="filter-item" placeholder="工程名称"/>
          </el-form-item>
          <el-form-item label="工程规模:" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="listQuery.scale"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option v-for="item in scaleList" :value="item.code" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工程状态:" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="listQuery.status"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option v-for="item in statusList" :value="item.code" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否整改暂停:" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="listQuery.rectifyStop"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option :value="0" label="正常"></el-option>
              <el-option :value="1" label="整改暂停"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="form-item">
            <el-button size="mini" icon="el-icon-refresh" @click="colse">重置</el-button>
            <el-button type="primary" size="mini" icon="el-icon-search" @click="getList">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </transition>

    <div class="table_content">
      <headerSearch addTitle="创建新工程" @handleAdd="handleAdd"
                    @creRefresh="getList"
                    @isSearch="showSearch = !showSearch"></headerSearch>
      <!--表格渲染-->
      <el-table ref="table" v-loading="loading" :data="list" border style="width: 100%;" border
                :header-cell-style="{background: '#F8F8F9', color: '#515a6e',padding: '6px 0 !important'}">
        <af-table-column prop="projectName" fixed="left" label="工程名称" />
        <af-table-column prop="orgName" label="机构名称" />
        <af-table-column prop="scale" label="工程规模(万元)" />
        <af-table-column prop="investment" label="总投资(元)" />
        <el-table-column prop="needBidding" label="是否需要招标" min-width="110">
          <template slot-scope="scope">
            {{{'0':'不需要招标', '1':'需要招标'}[scope.row.needBidding]}}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="工程状态">
          <template slot-scope="scope">
            {{scope.row.status | statusFilters}}
          </template>
        </el-table-column>
        <el-table-column prop="rectifyStop" label="是否整改暂停" min-width="110">
          <template slot-scope="scope">
            {{{'0':'正常', '1':'整改暂停'}[scope.row.rectifyStop]}}
          </template>
        </el-table-column>
        <el-table-column prop="estabTime" label="立项提交时间" min-width="140"/>
        <el-table-column prop="addTime" label="创建时间" min-width="140"/>
        <el-table-column fixed="right" label="操作" min-width="100">
          <template slot-scope="scope">
            <el-button class="btn" type="text" size="small"
                       @click="lookeEdit(scope.$index,scope.row)">编辑</el-button>
            <!--<el-button class="btn" type="text" size="small"-->
                       <!--@click="handleEdit(scope.$index,scope.row)">编辑</el-button>-->
            <!--<el-popover
              placement="top"
              width="200"
              trigger="manual"
              :ref="'popover-' + scope.row.id">
              <p>确定删除该数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.id].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="handleDel(scope.$index,scope.row)">确定</el-button>
              </div>
              <el-button class="btn" type="text" size="small" slot="reference"
                         @click="$refs[`popover-` + scope.row.id].doShow()">删除</el-button>
            </el-popover>-->
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination v-show="total>0" :total="total"
                  :page.sync="listQuery.pageNum"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getList" />
    </div>

    <!--表单添加-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工程名称:" prop="projectName" class="form-item">
              <el-input v-model="editForm.projectName" class="filter-item" placeholder="工程名称"/>
            </el-form-item>
            <el-form-item label="工程规模(万元):" prop="scale" class="form-item">
              <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.needBidding"
                         class="form-select" :popper-append-to-body="false" popper-class="select-popper">
                <el-option v-for="item in scaleList" :value="item.code" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="机构名称:" prop="orgName" class="form-item">
              <el-select v-model="editForm.orgName" placeholder="请选择" ref="selectReport"
                         popper-class="select-popper" class="form-select"
                         :popper-append-to-body="false">
                <el-option
                  :value="editForm.orgID"
                  :label="editForm.orgName"
                  style="
                width: 450px;
                height: 200px;
                overflow: auto;
                background-color: #fff;
              "
                >
                  <el-tree
                    :data="orgList"
                    :props="{value: 'orgID',label: 'orgName',}"
                    @node-click="handleNodeClick"
                  ></el-tree>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否需要招标:" prop="needBidding" class="form-item">
              <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.needBidding"
                         class="form-select" :popper-append-to-body="false" popper-class="select-popper">
                <el-option :value="0" label="不需要招标"></el-option>
                <el-option :value="1" label="需要招标"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总投资(元):" prop="investment" class="form-item">
              <el-input v-model="editForm.investment" class="filter-item" placeholder="总投资"/>
            </el-form-item>
            <el-form-item label="项目情况:" prop="situation	" class="form-item">
              <el-input v-model="editForm.situation" class="filter-item" placeholder="项目情况"
                        type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
            </el-form-item>
            <el-form-item label="工程建设目的:" prop="purpose	" class="form-item">
              <el-input v-model="editForm.purpose" class="filter-item" placeholder="工程建设目的"
                        type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
            </el-form-item>
            <el-form-item label="工程建设内容:" prop="content	" class="form-item">
              <el-input v-model="editForm.content" class="filter-item" placeholder="工程建设内容"
                        type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="dialogFormVisible=false">取消</el-button>
        <el-button type="primary" size="mini" @click="createData">
          {{dialogStatus == 'create'?'添加':'保存'}}
        </el-button>
      </div>
    </el-dialog>

    <amend-pwd v-if="fistFlag == 0"></amend-pwd>

  </div>
</template>

<script>
  import { doPost, doGet, doFile, orglistChild } from '@/api/index'
  import Pagination from '@/components/Pagination/index'
  import { mapGetters } from 'vuex'
  import headerSearch from '@/components/headerSearch/headerSearch'
  import {statusList, scaleList} from '@/utils/validate'

  export default {
    name: "index.vue",
    components: { Pagination, headerSearch },
    computed: {
      ...mapGetters([
        'name',
        'fistFlag',
        'userForm'
      ])
    },
    data() {
      return {
        loading: false,
        showSearch: true,
        statusList,
        scaleList,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          status: '',
          projectName: '',
          scale: '',
          rectifyStop: ''
        },
        total: 0,
        list: [
          {
            id: 0,
            projectName: '测试工程项目名称',
            orgName: '测试机构名称',
            scale: 20,
            investment: 10000,
            needBidding: 0,
            status: '00',
            estabTime: '2022-06-29 12:33:33'
          }
        ],
        dialogStatus: '',
        textMap:{
          update: '编辑',
          create: '新增'
        },
        dialogFormVisible: false,
        editForm: {
          projectName: '',
          scale: '',
          orgID: '',
          orgName: '',
          address: '',
          duration: '',
          investment: '',
          situation: '',
        },
        editFormRules: {
          orgName: [{ required: true, message: '请选择机构名称', trigger: 'blur' }],
          projectName: [{ required: true, message: '请填写项目名称', trigger: 'blur' }]
        },
        orgList: []
      }
    },
    created() {
      // this.getList()
      this.orgGetList()
    },
    methods: {
      colse() {
        this.listQuery = {
          pageNum: 1,
          pageSize: 20,
          status: '',
          projectName: '',
          scale: ''
        }
      },
      orgGetList(){
        orglistChild().then(res=> {
          this.orgList = res.data
        }).catch(error => {
          this.orgList = []
        })
      },
      lookeEdit(index,row) {
        this.$router.push({ name: 'projectUpeate', params: { id: row.id, type: 'update' }})
      },
      getList() {
        this.loading = true
        doPost('project/page', this.listQuery).then(res => {
          this.loading = false
          this.list = res.data.list
          this.total = res.data.total
        }).catch(error => {
          this.loading = false
          this.list = []
          this.total = 0
        })
      },
      //添加弹窗
      handleAdd() {
        this.$router.push({ name: 'projectUpeate', params: { id: 0,type: 'create' }})
        /*this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.editForm = {
          projectName: '',
          scale: '',
          orgID: '',
          orgName: '',
          address: '',
          duration: '',
          investment: '',
          situation: '',
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })*/
      },
      //编辑弹窗
      handleEdit(index, row) {
        if (row) {
          for (var key in row) {
            if (row[key] == null || typeof(row[key]) === "undefined" || row[key] == undefined) {
              // delete row[key];
              row[key] = ''
            }
          }
        }
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.editForm = Object.assign({}, row)
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      //提交表单
      createData() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doPost(`project/${this.dialogStatus == 'create' ? 'add' : 'update'}`,this.editForm).then(res => {
              if (res.code == 200){
                this.dialogFormVisible = false
                this.getList()
                this.$message.success({
                  message: this.dialogStatus == 'create'?'提交成功':'保存成功',
                  type: 'success'
                })
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
      //删除
      handleDel(index, row) {
        doPost('rfidreader/delete/'+row.id)
          .then(res => {
            this.$message.success({
              type: 'success',
              message: '删除成功'
            })
            this.list.splice(index, 1)
            this.total--
            this.$refs[`popover-` + row.id].doClose()
          }).catch(error => {
          this.$refs[`popover-` + row.id].doClose()
          this.$message.error(error.message)
        })
      },
      //选择部门
      handleNodeClick(val) {
        this.$set(this.editForm, "orgID", val.orgID);
        this.$set(this.editForm, "orgName", val.orgName);
        if (!val.children || val.children.length == 0) {
          this.$refs.selectReport.blur();
        }
      },
    },
    filters: {
      statusFilters(val) {
        return {
          '00':'立项',
          '01':'立项待审批',
          '02':'立项审批通过',
          '40':'开始招标',
          '41':'招标待审批',
          '42':'招标审批通过',
          '60':'项目建设开工',
          '61':'开工待审批',
          '62':'开工审批通过/建设',
          '80':'工程验收',
          '81':'验收待审批',
          '82':'验收审批通过',
          '99':'项目归档',
        }[val]
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
